package com.lonely.fx.dialog;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.Pair;

import java.util.Optional;
import java.util.function.Consumer;

/**
 * @date: 2022-08-01 23:30
 * @author: ztkj-hzb
 * @description: 自定义dialog 属性设置
 **/
public class CustomerDialogSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        HBox hBox = new HBox();
        Button button = new Button("点击打开登录框");
        hBox.getChildren().addAll(button);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                //自定义弹框

                Dialog<Pair<String, String>> pairDialog = new Dialog<>();

                pairDialog.setTitle("登录框");
                pairDialog.setHeaderText("自定义登录框");
                pairDialog.setGraphic(new ImageView(new Image("icons/lbxx.png", 30.0, 30.0, true, true)));

                GridPane gridPane = new GridPane();
                Label nameLabel = new Label("请输入用户名：");
                Label pwdLabel = new Label("请输入密码：");
                TextField nameTf = new TextField();
                TextField pwdTf = new TextField();

                gridPane.setHgap(20);
                gridPane.setVgap(20);
                gridPane.setAlignment(Pos.CENTER_LEFT);

                gridPane.add(nameLabel, 0, 0);
                gridPane.add(pwdLabel, 0, 1);
                gridPane.add(nameTf, 1, 0);
                gridPane.add(pwdTf, 1, 1);

                pairDialog.getDialogPane().setContent(gridPane);

                pairDialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
                pairDialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);

                pairDialog.setResultConverter(new Callback<ButtonType, Pair<String, String>>() {
                    @Override
                    public Pair<String, String> call(ButtonType param) {
                        if (param == ButtonType.OK) {
                            return new Pair<>(nameTf.getText(), pwdTf.getText());
                        }
                        return null;
                    }
                });

                //用户框注入焦点
                Platform.runLater(nameTf::requestFocus);


                Optional<Pair<String, String>> showAndWait = pairDialog.showAndWait();

                showAndWait.ifPresent(stringStringPair -> {

                    String name = stringStringPair.getKey();
                    String pwd = stringStringPair.getValue();

                    if ("admin".equals(name) && "admin".equals(pwd)) {
                        new Alert(Alert.AlertType.INFORMATION, "登录成功").showAndWait();
                    } else {
                        new Alert(Alert.AlertType.ERROR, "账号或密码错误").showAndWait();
                        pairDialog.showAndWait();
                    }

                });

            }
        });
    }
}
